<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>物料档案</title>
      <link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet" >
      <script src="/static/bootstrap/js/bootstrap.bundle.min.js" ></script>
      {% load widget_tweaks %}  <!-- 加载widget_tweaks过滤器 -->
{% load url_exists %}  <!-- 加载widget_tweaks过滤器 -->
  </head>
<body>

<div class="container">
    {% if messages %}
  {% for message in messages %}
    <div class="alert alert-{{ message.tags }}">
      {{ message }}
    </div>
  {% endfor %}
{% endif %}

    <div class="back-link">
        <a href="javascript:history.back()" class="btn-back">← 返回上一级</a>
    </div>
<form method="post" id="form">
<div class="search-form d-flex align-items-center gap-2 mb-3 " >
 {% include "utils/printtools.html"%}
</div>

 <!-- 全局错误信息 -->
        {% if form.non_field_errors %}
            <div class="alert alert-danger">
                {% for error in form.non_field_errors %}
                    <p>{{ error }}</p>
                {% endfor %}
            </div>
        {% endif %}
     {% csrf_token %}
     <!-- pk -->
{#       <input name="pk" type="hidden" value="{{ pk }}">#}
<input name="pk" type="hidden" value="{{ pk|default:'' }}">
<!-- 渲染物料类型单选框（来自表单类） -->
    <div class="mb-3 row">
        <label class="col-sm-2 col-form-label">{{ form.material_type.label }}</label>

        <div class="col-sm-6">
            {% for radio in form.material_type %}
                <div class="form-check form-check-inline">

        <input
            type="radio"
            name="material_type"
            value="{{ radio.data.value }}"
            id="{{ radio.id_for_label }}"
            class="form-check-input"
            {% if radio.data.value == form.material_type.value %}checked{% endif %}
            {% if pk %}disabled{% endif %}
        >
                    <label class="form-check-label" for="{{ radio.id_for_label }}">
                        {{ radio.choice_label }}  <!-- 渲染选项文本 -->
                    </label>
                </div>
            {% endfor %}
            {% if form.material_type.errors %}
                <div class="form-error text-danger">{{ form.material_type.errors.0 }}</div>
            {% endif %}
        <!-- 隐藏字段：用于提交实际值（编辑时必须添加） -->
            {% if pk %}
                <input type="hidden"
                       name="material_type"
                       value="{{ form.material_type.value }}">
            {% endif %}
        </div>
    </div>
    <div class="mb-3 row">

        <label for="{{ form.standard.id_for_label }}" class="col-sm-2 col-form-label">
            {{ form.standard.label }}
        </label>
        <div class="col-sm-5">
            {{ form.standard|add_class:"form-select" }}
        </div>
        {% if form.standard.errors %}
            <div class="form-error text-danger">{{ form.standard.errors.0 }}</div>
        {% endif %}
    </div>
    <div class="mb-3 row">

        <label for="{{ form.storage_condition.id_for_label }}" class="col-sm-2 col-form-label">
            {{ form.storage_condition.label }}
        </label>
        <div class="col-sm-5">
            {{ form.storage_condition|add_class:"form-select" }}
        </div>
        {% if form.storage_condition.errors %}
            <div class="form-error text-danger">{{ form.storage_condition.errors.0 }}</div>
        {% endif %}
    </div>



        <!-- 手动渲染所有字段，统一Bootstrap样式 -->
        <div class="mb-3 row">
            <label for="{{ form.name.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.name.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.name|add_class:"form-control" }}  <!-- 添加Bootstrap样式类 -->
            </div>
            {% if form.name.errors %}
                <div class="form-error">{{ form.name.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
            <label for="{{ form.jian_pin.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.jian_pin.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.jian_pin|add_class:"form-control col-sm-2" }}
             </div>
            {% if form.jian_pin.errors %}
                <div class="form-error">{{ form.jian_pin.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
        <label for="{{ form.quan_pin.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.quan_pin.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.quan_pin|add_class:"form-control col-sm-2" }}
             </div>

            {% if form.quan_pin.errors %}
                <div class="form-error">{{ form.quan_pin.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
            <label for="{{ form.gui_ge.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.gui_ge.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.gui_ge|add_class:"form-control col-sm-2" }}
             </div>

            {% if form.gui_ge.errors %}
                <div class="form-error">{{ form.gui_ge.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
            <label for="{{ form.code.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.code.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.code|add_class:"form-control col-sm-2" }}
             </div>

            {% if form.code.errors %}
                <div class="form-error">{{ form.code.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
        <label for="{{ form.state.id_for_label }}" class="col-sm-2 col-form-label">
            {{ form.state.label }}
        </label>
        <div class="col-sm-5">
            {{ form.state|add_class:"form-select" }}
        </div>
            {% if form.state.errors %}
                <div class="form-error">{{ form.state.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
             <label for="{{ form.version.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.version.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.version|add_class:"form-control" }}  <!-- 添加Bootstrap样式类 -->
            </div>

            {% if form.version.errors %}
                <div class="form-error">{{ form.version.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
            <label for="{{ form.chan_di.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.chan_di.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.chan_di|add_class:"form-control" }}  <!-- 添加Bootstrap样式类 -->
            </div>

            {% if form.chan_di.errors %}
                <div class="form-error">{{ form.chan_di.errors.0 }}</div>
            {% endif %}
        </div>

        <div class="mb-3 row">
        <label for="{{ form.remark.id_for_label }}" class="col-sm-2 col-form-label">
                {{ form.remark.label }}  <!-- 显示标签文本 -->
            </label>
            <div class="col-sm-5">
            {{ form.remark|add_class:"form-control" }}  <!-- 添加Bootstrap样式类 -->
            </div>

            {% if form.remark.errors %}
                <div class="form-error">{{ form.remark.errors.0 }}</div>
            {% endif %}
        </div>


    <!-- 提交按钮 -->
    <button type="submit" class="btn btn-primary">提交</button>
</form>

<!-- 显示全局错误信息（可选） -->
{% if form.non_field_errors %}
    <div class="errors">
        {% for error in form.non_field_errors %}
            <p>{{ error }}</p>
        {% endfor %}
    </div>
{% endif %}
</div>


</body>
<!-- 可选：添加CSS美化返回按钮 -->
<style>
    .back-link {
        margin: 10px 0 20px;
    }
    .btn-back {
        color: #0066cc;
        text-decoration: none;
        font-size: 14px;
    }
    .btn-back:hover {
        text-decoration: underline;
    }
</style>
</html>